package cn.zero.letaomallspringboot.controller;

import cn.zero.letaomallspringboot.common.PageResult;
import cn.zero.letaomallspringboot.common.Result;
import cn.zero.letaomallspringboot.entity.Brand;
import cn.zero.letaomallspringboot.service.IBrandService;
import cn.zero.letaomallspringboot.service.impl.BrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    private static final Logger log = LoggerFactory.getLogger(BrandController.class);
    private final IBrandService brandService;

    public BrandController(IBrandService brandService) {
        this.brandService = brandService;
    }

    /**
     * 分页查询品牌列表
     * @param page 当前页码
     * @param size 每页条数
     * @return 分页结果
     */
    @GetMapping("/list")
    public Result<PageResult<Brand>> getBrandPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {

        log.info("分页查询品牌列表，页码：{}，每页条数：{}", page, size);
        PageResult<Brand> pageResult = brandService.getBrandPage(page, size);
        return Result.success(pageResult, "品牌列表查询成功");
    }

    /**
     * 根据ID查询品牌详情
     */
    @GetMapping("/{id}")
    public Result<Brand> getBrandById(@PathVariable Integer id) {
        log.info("根据ID查询品牌：{}", id);
        Brand brand = brandService.getById(id);
        return Result.success(brand, "查询成功");
    }

    /**
     * 新增品牌
     */
    @PostMapping
    public Result<Boolean> addBrand(@RequestBody Brand brand) {
        log.info("新增品牌：{}", brand);
        boolean success = brandService.addBrand(brand);
        return success ? Result.success(true, "新增成功") : Result.fail(500, "新增失败");
    }

    /**
     * 更新品牌
     */
    @PutMapping
    public Result<Boolean> updateBrand(@RequestBody Brand brand) {
        log.info("更新品牌：{}", brand);
        boolean success = brandService.updateBrand(brand);
        return success ? Result.success(true, "更新成功") : Result.fail(500, "更新失败");
    }

    /**
     * 删除品牌
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteBrand(@PathVariable Integer id) {
        log.info("删除品牌：{}", id);
        boolean success = brandService.deleteBrand(id);
        return success ? Result.success(true, "删除成功") : Result.fail(500, "删除失败");
    }

    /**
     * 批量删除品牌
     */
    @DeleteMapping("/batch")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        log.info("批量删除品牌：{}", ids);
        boolean success = brandService.deleteBatch(ids);
        return success ? Result.success(true, "批量删除成功") : Result.fail(500, "批量删除失败");
    }

    /**
     * 更新品牌状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        log.info("更新品牌状态：id={}, status={}", id, status);
        boolean success = brandService.updateStatus(id, status);
        return success ? Result.success(true, "状态更新成功") : Result.fail(500, "状态更新失败");
    }
}
